package org.smartopen.unionpay.admin.controller.openapi;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.smartframework.core.api.Resp;
import org.smartframework.core.util.IpUtils;
import org.smartframework.core.util.StrUtil;
import org.smartframework.unionpay.constant.PayConstant;
import org.smartframework.unionpay.domain.request.SmartPayMicropayRequest;
import org.smartframework.unionpay.domain.request.SmartPayQueryRequest;
import org.smartframework.unionpay.domain.request.SmartPayRefundRequest;
import org.smartframework.unionpay.domain.request.SmartPayUnifiedorderRequest;
import org.smartframework.unionpay.domain.result.SmartPayMicropayResult;
import org.smartframework.unionpay.domain.result.SmartPayQueryResult;
import org.smartframework.unionpay.domain.result.SmartPayRefundResult;
import org.smartframework.unionpay.domain.result.SmartPayUnifiedorderResult;
import org.smartopen.unionpay.openapi.service.UnionPayService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@Api(tags = {"支付接口"})
@RestController
@RequiredArgsConstructor
@RequestMapping("/openapi/tran")
public class OpenApiTranController {

    private final UnionPayService unionPayService;

    @PostMapping("unifiedorder")
    @ApiOperation("统一支付")
    public Resp<SmartPayUnifiedorderResult> unifiedorder(HttpServletRequest req,
                                         @RequestParam(defaultValue = PayConstant.PAY_TYPE_ALIPAY) String payType,
                                         @RequestParam(defaultValue = "2088002785851996") String openid) {
        SmartPayUnifiedorderRequest request = new SmartPayUnifiedorderRequest();
        request.setTotalFee(1l);
        request.setBody("test");
        request.setAttach("test");
        request.setOpenId(openid);
        request.setPayType(payType);
        request.setOutOrderNo(uuid());
        request.setSpbillCreateIp(IpUtils.getRealIp(req));
        return Resp.ok(unionPayService.unifiedorder(request));
    }

    @PostMapping("micropay")
    @ApiOperation("二维码支付")
    public Resp<SmartPayMicropayResult> micropay(HttpServletRequest req, @RequestParam String code) {
        SmartPayMicropayRequest request = new SmartPayMicropayRequest();
        request.setAuthCode(code);
        request.setTotalFee(1l);
        request.setBody("test");
        request.setAttach("test");
        request.setOutOrderNo(uuid());
        request.setSpbillCreateIp(IpUtils.getRealIp(req));
        return Resp.ok(unionPayService.micropay(request));
    }

    @ApiOperation(value = "订单查询")
    @PostMapping("queryOrder")
    public Resp queryOrder(@RequestParam String outOrderNo) {
        SmartPayQueryRequest request = new SmartPayQueryRequest();
        request.setOutTradeNo(outOrderNo);
        SmartPayQueryResult queryResult = this.unionPayService.queryOrder(request);
        return Resp.ok(queryResult);
    }

    /**
     * 订单退款
     *
     * @param request
     * @return
     */
    @PostMapping("refundOrder")
    @ApiOperation("订单退款")
    public Resp<SmartPayRefundResult> refundOrder(@RequestBody SmartPayRefundRequest request) {
        return Resp.ok(unionPayService.refundOrder(request));
    }

    private String uuid() {
        return StrUtil.uuid().replace("-", "");
    }
}
